<?php

namespace App\Trip\Plane\Calendar;

use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Validator;

class CalendarData implements Arrayable
{
    private array $data;

    /**
     * @throws Exception
     */
    public function __construct(array $data)
    {
        $validator = Validator::make($data, [
            'date'            => "required|date", // 日期
            'price_desc'      => "required",// 价格
            'plane'           => "required|array", // 航班数据
            'plane.from_code' => "required",
            'plane.to_code'   => "required",
            'plane.flight_no' => "required",
        ]);
        if ($validator->fails()) {
            throw new Exception($validator->errors()->first());
        }
        $this->data = $data;
    }


    public function toArray(): array
    {
        return $this->data;
    }
}